package com.api.gbhg.daiyanlu.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.FoBudgetProcessDao;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import weaver.conn.RecordSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * 非独立核算单位财务预算核批
 * Mr.dyl
 */
public class BudgetApprovalActionApi {
    @POST
    @Path("/getFinancea")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getFinance(Map<String,Object> param){
        if (param.containsKey("dw") && param.get("dw") instanceof Boolean && !(Boolean)param.get("dw")) {
            return JSON.toJSONString((Object)null);
        } else {
            Integer pageNum = 1;
            Integer pageSize = 5;
            boolean state = true;
            if (param.containsKey("pageNum") && param.get("pageNum") instanceof Integer && (Integer) param.get("pageNum") > 0)
                pageNum = (Integer) param.get("pageNum");
            if (param.containsKey("pageSize") && param.get("pageSize") instanceof Integer && (Integer) param.get("pageSize") > 0)
                pageSize = (Integer) param.get("pageSize");
            if (param.containsKey("state") && param.get("state") instanceof Boolean)
                state = (boolean) param.get("state");
            int nd = getNd(param);
            String dw = "2";
            if (param.containsKey("dw") && param.get("dw") instanceof Integer) {
                dw = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
            }
            RecordSet data = FoBudgetProcessDao.getMainTable(pageNum, pageSize, nd, dw, state, 0);
            Map<String, Object>[] map = new HashMap[data.getArray().size()];
            int i = 0;
            while (data.next()) {
                map[i] = new HashMap<String, Object>();
                map[i].put("xmmc", data.getString("xmmin"));
                map[i].put("yszje", data.getString("yszje"));
                map[i].put("cjsj", data.getString("cjsj"));
                i++;
            }
            com.api.gbhg.zengping.page.PageInfo<String> pageInfo = new com.api.gbhg.zengping.page.PageInfo<>();
            int cut = FoBudgetProcessDao.getMainTableCount(nd, dw, state, 0);
            pageInfo.setPageNum(pageNum);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTotal(cut);
            pageInfo.setList(map);

            return JSON.toJSONString(pageInfo);
        }
    }

    public int getNd(Map<String,Object> param){
        if(param.containsKey("nd") && param.get("nd") instanceof Integer )
            return  (Integer) param.get("nd");
        else
            return LocalDate.now().getYear();
    }
}
